//
//  UIScrollView+RefreshHeader.m
//  Category
//
//  Created by Burn on 2020/1/6.
//  Copyright © 2020 menksoft. All rights reserved.
//

#import "UIScrollView+Refresh.h"
#import "UIView+Extension.h"
#import <objc/runtime.h>


@implementation UIScrollView (RefreshHeader)


#pragma mark - header
static const char RefreshHeaderKey = '\0';

- (void)setMk_header:(MKRefreshHeaderView *)mk_header{
    if (mk_header != self.mk_header) {
        [self.mk_header removeFromSuperview];
        [self insertSubview:mk_header atIndex:0];
        
        // 存储新的
        objc_setAssociatedObject(self, &RefreshHeaderKey, mk_header, OBJC_ASSOCIATION_RETAIN);
    }
}

- (MKRefreshHeaderView *)mk_header{
    return objc_getAssociatedObject(self, &RefreshHeaderKey);
}




#pragma mark - footer
static const char RefreshFooterKey = '\0';

- (void)setMk_footer:(MKRefreshFooterView *)mk_footer{
    if (mk_footer != self.mk_footer) {
        [self.mk_footer removeFromSuperview];
          [self insertSubview:mk_footer atIndex:0];
        // 存储新的
        objc_setAssociatedObject(self, &RefreshFooterKey, mk_footer, OBJC_ASSOCIATION_RETAIN);
    }
}

- (MKRefreshFooterView *)mk_footer{
    return objc_getAssociatedObject(self, &RefreshFooterKey);
}




#pragma mark - left
static const char RefreshLeftKey = '\0';

- (void)setMk_left:(MKRefreshLeftView *)mk_left{
    if (mk_left != self.mk_left) {
        [self.mk_left removeFromSuperview];
        [self insertSubview:mk_left atIndex:0];
        
        // 存储新的
        objc_setAssociatedObject(self, &RefreshLeftKey, mk_left, OBJC_ASSOCIATION_RETAIN);
    }
}

- (MKRefreshLeftView *)mk_left{
    return objc_getAssociatedObject(self, &RefreshLeftKey);
}


#pragma mark - right
static const char RefreshRightKey = '\0';

- (void)setMk_rigth:(MKRefreshRightView *)mk_rigth{
    if (mk_rigth != self.mk_rigth) {
        [self.mk_left removeFromSuperview];
        [self insertSubview:mk_rigth atIndex:0];
        
        // 存储新的
        objc_setAssociatedObject(self, &RefreshRightKey, mk_rigth, OBJC_ASSOCIATION_RETAIN);
    }
}

- (MKRefreshRightView *)mk_rigth{
    return objc_getAssociatedObject(self, &RefreshRightKey);
}









#pragma mark - other
- (NSInteger)mk_totalDataCount
{
    NSInteger totalCount = 0;
    if ([self isKindOfClass:[UITableView class]]) {
        UITableView *tableView = (UITableView *)self;
        
        for (NSInteger section = 0; section < tableView.numberOfSections; section++) {
            totalCount += [tableView numberOfRowsInSection:section];
        }
    } else if ([self isKindOfClass:[UICollectionView class]]) {
        UICollectionView *collectionView = (UICollectionView *)self;
        
        for (NSInteger section = 0; section < collectionView.numberOfSections; section++) {
            totalCount += [collectionView numberOfItemsInSection:section];
        }
    }
    return totalCount;
}


@end
